import {
  diamondRecharge,
  sendGift,
  sendGiftBroadCast,
  reqSendAllHby,
  tipSinger,
  reqOrderSong,
} from "../../api/recharge/recharge";
import { rechageHbkd } from "../../api/hbkdRecharge/hbkdRecharge";
import { wxPay } from "../../api/pay";
import { timeoutTask, showWxToast } from "../../utils/index";
import { setOrigin } from "../../utils/storage";
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    total: 0,
    userId: "",
    openId: "",
    origin: "",
    recommendGiftId: "",
    superDanmuId: "",
    superDanmuWish: "",
    hbyNum: "",
    hbyType: "",
    hbyContent: "",
    singerId: "",
    contentJson: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if (options.total) {
      this.setData({
        total: options.total,
      });
    }
    if (options.userId) {
      this.setData({
        userId: options.userId,
      });
    }
    if (options.openId) {
      this.setData({
        openId: options.openId,
      });
    }
    if (options.origin) {
      this.setData({
        origin: options.origin,
      });
    }
    if (this.data.origin === "sendRecommendGift") {
      this.setData({
        recommendGiftId: options.giftId,
      });
      this.sendRecommendGift();
    } else if (this.data.origin === "sendTipGift") {
      const tipContent = {
        singername: options.singername,
        desknum: options.desknum,
        content: options.content,
      };
      this.setData({
        recommendGiftId: options.giftId,
        singerId: options.singerId,
        contentJson: JSON.stringify(tipContent),
      });
      this.sendTipGift();
    } else if (this.data.origin === "rechargeHbkd") {
      this.rechargeHbkd();
    } else if (this.data.origin === "rechargeHby") {
      // 全场红包雨
      this.setData({
        hbyNum: options.hbyNum,
        hbyType: options.hbyType,
        hbyContent: options.hbyContent,
      });
      this.sendHbyAll();
    } else if(this.data.origin === "orderSong") {
      this.setData({
        songId: options.songId,
        orderSongFormJson: options.orderSongFormJson,
      });
      this.orderSong();
    } else {
      this.rechargeDiamonds();
    }
  },
  // 打赏歌手支付流程
  sendTipGift: function () {
    tipSinger({
      openid: this.data.openId,
      giftId: this.data.recommendGiftId,
      singerId: this.data.singerId,
      contentJson: this.data.contentJson,
      USER_ID: this.data.userId,
    })
      .then((res) => {
        console.log(res);
        return wxPay(res);
      })
      .then(() => {
        showWxToast("打赏成功");
        app.globalData.isH5NeedRefresh = true;
        if (this.data.origin) {
          setOrigin(this.data.origin);
        }
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
      })
      .catch((err) => {
        console.log(err);
        showWxToast("打赏失败");
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
      });
  },
  //充值完直接发礼物
  sendRecommendGift: function () {
    diamondRecharge({
      total: this.data.total,
      userId: this.data.userId,
      openId: this.data.openId,
    })
      .then((res) => {
        console.log(res);
        return wxPay(res);
      })
      .then(() => {
        return sendGift({
          giftId: this.data.recommendGiftId,
        });
      })
      .then(() => {
        showWxToast("发送成功");
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
        return sendGiftBroadCast({
          giftId: this.data.recommendGiftId,
        });
      })
      .catch((err) => {
        console.log(err);
        showWxToast("发送失败");
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
      });
  },
  //充值喵钻
  rechargeDiamonds: function () {
    diamondRecharge({
      total: this.data.total,
      userId: this.data.userId,
      openId: this.data.openId,
    })
      .then((res) => {
        console.log(res);
        return wxPay(res);
      })
      .then(() => {
        showWxToast("充值成功");
        app.globalData.isH5NeedRefresh = true;
        if (this.data.origin) {
          setOrigin(this.data.origin);
        }
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
      })
      .catch((err) => {
        console.log(err);
        showWxToast("充值失败");
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
      });
  },
  // 充值红包口袋
  rechargeHbkd: function () {
    rechageHbkd({
      total: this.data.total,
      userId: this.data.userId,
      openId: this.data.openId,
    })
      .then((res) => {
        console.log(res);
        return wxPay(res);
      })
      .then(() => {
        showWxToast("充值成功");
        app.globalData.isH5NeedRefresh = true;
        if (this.data.origin) {
          setOrigin(this.data.origin);
        }
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
      })
      .catch((err) => {
        console.log(err);
        showWxToast("充值失败");
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
      });
  },
  // 全场红包雨
  sendHbyAll() {
    reqSendAllHby({
      totalfee: this.data.total,
      openid: this.data.openId,
      data1: this.data.hbyNum,
      data2: this.data.hbyContent,
      data3: this.data.hbyType,
      USER_ID: this.data.userId,
    })
      .then((res) => {
        console.log(res);
        return wxPay(res);
      })
      .then(() => {
        showWxToast("充值成功");
        app.globalData.isH5NeedRefresh = true;
        if (this.data.origin) {
          setOrigin(this.data.origin);
        }
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
      })
      .catch((err) => {
        console.log(err);
        showWxToast("充值失败");
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
      });
  },
  /**
   * 点歌支付流程
   * 点歌接口
   * 点歌接口需要参数：songId、orderSongFormJson
   */
  orderSong: function () {
    // 解析json
    reqOrderSong({
      songId: this.data.songId,
      orderSongFormJson: this.data.orderSongFormJson,
      userId: this.data.userId,
      openid: this.data.openId,
    })
      .then((res) => {
        console.log(res);
        return wxPay(res);
      })
      .then(() => {
        showWxToast("点歌成功");
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
      })
      .catch((err) => {
        console.log(err);
        showWxToast("点歌失败");
        timeoutTask(() => {
          wx.navigateBack();
        }, 500);
      });
  },
});
